//
//  FNumberJust.swift
//  xzseller
//
//  Created by zhuxietong on 2021/2/4.
//

import Foundation
import Eelay

open class FNumberJustContent:PriceStepField,UITextFieldDelegate{
    open var stepSize:CGSize{
        get{
            return [128,25]
        }
    }

    open override func didLayoutRules() {
        numT.keyboardType = .numberPad
        addBt.tag = 1
        reduceBt.tag = -1
        
        addBt.addTarget(self, action: #selector(updateNum(sender:)), for: .touchUpInside)
        reduceBt.addTarget(self, action: #selector(updateNum(sender:)), for: .touchUpInside)
        
        addBt.ui.font(size: 14).ncolor(color: .white).ntitle(title: "+").buttonColor(color: ._tint)
        reduceBt.ui.font(size: 14).ncolor(color: .white).ntitle(title: "-").buttonColor(color: ._tint)
        numT.addTarget(self, action: #selector(changeText), for: .editingChanged)
    }
    open override func addLayoutRules() {
        self.eelay = [
            [reduceBt,[ee.T.L.B,[0.+1000,0.+1000,0.+1000]],28.+1000],
            [addBt,[ee.T.R.B,[0.+1000,0.+1000,0.+1000]],28.+1000],
            [numT,[ee.T.B],"28",[reduceBt,ee.R,ee.L,4.+1000],[addBt,ee.L,ee.R,-4.+1000]],
        ]
        numT.text = "1"
        numT.delegate = self
        numT.textAlignment = .center
        addBt.ui.font(size: 14).ncolor(color: ._foreground).ntitle(title: "+").set(radius: 2, width: nil, color: nil)
        reduceBt.ui.font(size: 14).ncolor(color: ._foreground).ntitle(title: "-").set(radius: 2, width: nil, color: nil)
        numT.ui.bfont16.text(color: ._tint).center.background(color: ._tableBackground).set(radius: 2, width: nil, color: nil)

        numT.tintColor = UIColor._tint
        
    }
    open override func textFieldDidEndEditing(_ textField: UITextField) {
        let t = textField.text ?? ""
        if t.len < 1{
            textField.text = "1"
            changeNumValue(1)
        }
    }
    
    

    @objc func changeText(){
//            print("LLLLS",numT.text)
        let n = numT.text ?? "1"
        changeNumValue(n.CGFloatV)
        
    }
}


open class FSimpleNumberJust<Step:FNumberJustContent>:FNumberJust<Step>{
    
    open override func specGetter() -> _Spec {
        return stepNode["\(stepF.stepSize.height)",stepF.stepSize.width]
    }
}

open class FNumberJust<Step:FNumberJustContent>:FField{
    open override var defaultProps: [FProps]{
        get{
            
            let v:FVerifyMethod = {v in
                let n = "\(v)".IntValue
                if n < 0{
                    return .nil
                }
                return .success(value: v)
            }
            return [.type(.setting),.verify(.method(v))]
        }
    }
    
    
    public let stepNode = MoDisplayNode { () -> UIView in
        let v = Step()
        return v
    }
    
    open var stepF:Step{
        return stepNode.view as! Step
    }
    
    open override func addLayoutRules() {
        stepF.changeNumValue = {
            [weak self] f in
            self?.update(value: "\(f)", edit: .input)
        }
    }
    open override func didLoadProps() {
        super.didLoadProps()
        stepF.numT.text = "\(value)"
        
    }
    
    
    
    open override func specGetter() -> _Spec {
        let spec = _Spec.inset([], [10,16], child: .stack(["h|",&-.center,_justfy(.space_between)], childs: [
            lableNode.Spec,
            stepNode["\(stepF.stepSize.height)",stepF.stepSize.width],
        ]))
        return spec
    }
}
